#include "namemap.h"
#include <string.h>

struct NameListType{
    char content[64];
    NameListType* next;
};

NameListType* head = 0;

void clearNamemap(){
    NameListType* current, *next;
    current = head;
    while (current != 0){
        next = current->next;
        delete current;
        current = next;
    }
    head = 0;
}

int nameToId(char * name){
    int i = 0;
    NameListType* current = head, *prev = 0;
    while (current != 0){
        if (strcmp(current->content, name) == 0){
            return i;
        }
        prev = current;
        current = current->next;
        i++;
    }
    if (prev == 0){
        head = new NameListType();
        strcpy(head->content, name);
        head->next = 0;
    }else{
        prev->next = new NameListType();
        strcpy(prev->next->content, name);
        prev->next->next = 0;
    }
    return i;
}

void idToName(int id, char * name){
    int i = 0;
    name[0] = '\0';
    NameListType* current = head;
    for (i = 0; i < id; i++){
        if (current == 0) return;
        current = current->next;
    }
    if (current == 0) return;
    strcpy(name, current->content);
}

